package com.yuri.news.jpa;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import com.yuri.news.entity.News;

/**
 * 新闻jpa
 * @author Yuri
 */
public interface NewsRepository extends JpaRepository<News, Long>, JpaSpecificationExecutor<News> {
	/**
	 * 根据标题模糊查询
	 * @param title		标题
	 * @return
	 */
	List<News> findByTitleLike(String title);
	/**
	 * 根据标题模糊查询（分页）
	 * @param title		标题
	 * @param page		分页信息
	 * @return
	 */
	Page<News> findByTitleLike(String title, Pageable page);
	
	/**
	 * 根据分类名查询新闻
	 * @param depaName
	 * @return
	 */
	@Query(value = "SELECT * FROM t_news INNER JOIN t_classify ON t_news.classify_id = t_classify.id where t_classify.class_name = ?1", nativeQuery = true)
	List<News> findNewsByClassName(String className);
	
	/**
	 * 根据分类名查询新闻
	 * @param className		类名
	 * @param page			分页信息
	 * @return
	 */
	@Query(value = "SELECT * FROM t_news INNER JOIN t_classify ON t_news.classify_id = t_classify.id where t_classify.class_name = ?1", 
			countQuery = "SELECT count(*) FROM t_news INNER JOIN t_classify ON t_news.classify_id = t_classify.id where t_classify.class_name = ?1",
			nativeQuery = true)
	Page<News> findNewsByClassName(String className, Pageable page);
	
	/**
	 * 根据分类Id查询新闻
	 * @param classId	分类Id
	 * @return
	 */
	@Query(value = "SELECT * FROM t_news where t_news.classify_id = ?1", nativeQuery = true)
	List<News> findNewsByClassId(Long classId);
	
	/**
	 * 根据分类名查询新闻
	 * @param className		分类Id
	 * @param page			分页信息
	 * @return
	 */
	@Query(value = "SELECT * FROM t_news where t_news.classify_id = ?1",
			countQuery = "SELECT count(*) FROM t_news where t_news.classify_id = ?1",
			nativeQuery = true)
	Page<News> findNewsByClassId(Long classId, Pageable page);
}
